//
//  DialectsViewController.m
//  DialectShare
//
//  Created by timmy zhong on 3/24/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "DialectsViewController.h"
#import "DialectProxy.h"
#import "DialectDB.h"
#import "Dialect.h"
#import "DialectCell.h"
#import "ListenViewController.h"

@interface DialectsViewController ()
@end

@implementation DialectsViewController
@synthesize tableView = _tableView, proxy = _proxy, db = _db, dialects = _dialects;
- (id)init
{
    self = [super initWithNibName:@"DialectsViewController" bundle:nil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc
{
    self.tableView = nil;
    self.proxy.delegate = nil;
    self.proxy = nil;
    self.dialects = nil;
    self.db = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"我要说" style:UIBarButtonItemStylePlain target:nil action:nil] autorelease];
    
    _proxy = [[DialectProxy alloc] initWithDelegate:self];
    _db = [[DialectDB alloc] init];
    _dialects = [[NSMutableArray alloc] initWithCapacity:20];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


#pragma mark - Table view datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dialects count];
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return DialectCellHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[[DialectCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    Dialect *dialect = [_dialects objectAtIndex:indexPath.row];
    [((DialectCell *)cell) showDialect:dialect]; 
    
    return cell;
}

#pragma mark - table delggate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Dialect *dialect = [self.dialects objectAtIndex:indexPath.row];
    ListenViewController *controller = [[ListenViewController alloc] init];
    controller.dialect = dialect;
    [self.navigationController pushViewController:controller animated:YES];
    [controller release];
}



#pragma mark - proxy delegate
- (void)requestFailWithProxy:(Proxy *)proxy
{
    self.dialects = [self getDialects];
    [self.tableView reloadData];
}

- (void)requestFinishWithProxy:(Proxy *)proxy
{
    self.dialects = [self getDialects];
    [self.tableView reloadData];
}


- (NSArray *)getDialects
{
    return nil;
}

@end
